package edu.dayu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.DepartmentMapper;
import edu.dayu.model.hosp.Department;
import edu.dayu.service.DepartmentService;
import edu.dayu.vo.hosp.DepartmentQueryVo;
import edu.dayu.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);

        QueryWrapper<Department> wrapper = new QueryWrapper<Department>().eq("hoscode", department.getHoscode()).eq("depcode", department.getDepcode());
        Department targetDepartment = departmentMapper.selectOne(wrapper);
        if (null != targetDepartment) {
            //copy不为null的值，该方法为自定义方法
            BeanUtils.copyProperties(department, targetDepartment, Department.class);
            targetDepartment.setUpdateTime(new Date());

            departmentMapper.updateById(targetDepartment);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);

            departmentMapper.insert(department);
        }
    }

    @Override
    public PageInfo<Department> selectPage(Integer page, Integer size, DepartmentQueryVo departmentQueryVo) {
        PageHelper.startPage(page,size);

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);

        List<Department> departments = departmentMapper.selectList(new QueryWrapper<Department>(department).orderByDesc("id"));
        PageInfo<Department> pageInfo = new PageInfo<>(departments);
        return pageInfo;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        QueryWrapper<Department> wrapper = new QueryWrapper<Department>().eq("hoscode", hoscode).eq("depcode", depcode);
        Department department = departmentMapper.selectOne(wrapper);
        if (null != department){
            departmentMapper.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);

        List<Department> departmentList = departmentMapper.selectList(new QueryWrapper<>(departmentQuery));
        Map<String, List<Department>> listMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        for (Map.Entry<String, List<Department>> entry : listMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deparment1List.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : deparment1List) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            departmentVo1.setChildren(children);
            result.add(departmentVo1);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {

        QueryWrapper<Department> wrapper = new QueryWrapper<Department>().eq("hoscode", hoscode).eq("depcode", depcode);
        Department department = departmentMapper.selectOne(wrapper);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {

        QueryWrapper<Department> wrapper = new QueryWrapper<Department>().eq("hoscode", hoscode).eq("depcode", depcode);
        Department department = departmentMapper.selectOne(wrapper);
        return department;
    }
}
